<?php
/**
 * Layer for the use of MySQL DataBases
 * @copyright inovia
 */
class sql {

    private $connection;

    public function __construct($host,$username,$password,$database,$port){
        if(!($this->connection = @mysql_pconnect($host.":".$port,$username,$password)))
            throw new Exception("Unable to connect to the host \"".$host.":".$port."\".");
        if(!mysql_select_db($database, $this->connection))
            throw new Exception("Unable to connect to the database \"".$database."\".");
    }
    
    public function errno() {
        return mysql_errno($this->connection);
    }
    
    public function query($request){
        if(!($res = @mysql_query($request,$this->connection)))
            throw new Exception("Unable to execute the request \"".$request."\".");
        else return new sqlResult($res);
    }

    public function get_host_info() {
        return mysql_get_host_info($this->connection);
    }
    
    public function close() {
        return mysql_close($this->connection);
    }
    
    public function insert_id() {
        return mysql_insert_id();
    }
}

class sqlResult {
    
    private $result;
    
    public function __construct($result){    
        $this->result = $result;
    }
    
    public function fetch_object(){
        return mysql_fetch_object($this->result);        
    }
    
    public function fetch_row(){
        return mysql_fetch_row($this->result);        
    }
    
    public function fetch_array(){
        return mysql_fetch_array($this->result);        
    }
    
    public function fetch_assoc(){
        return mysql_fetch_assoc($this->result);        
    }
    
    public function num_rows(){
        return mysql_num_rows($this->result);
    }
    
    public function affected_rows() {
    		return mysql_affected_rows();
    }
    
    public function free_result() {
        mysql_free_result($this->result);
    }
    
    public function num_fields() {
         return mysql_num_fields($this->result);
    }
    
    public function fetch_field($i) {
         return mysql_fetch_field($this->result,$i);
    }
}
?>
